package com.ajmd.onlineexam.controller;

import com.ajmd.onlineexam.baseUtil.CommonResult;
import com.ajmd.onlineexam.config.MultiRequestBody;
import com.ajmd.onlineexam.entity.ExamPO;
import com.ajmd.onlineexam.entity.PersonAnswerPO;
import com.ajmd.onlineexam.entity.PersonExamPO;
import com.ajmd.onlineexam.entity.SearchParam;
import com.ajmd.onlineexam.enumeration.AppExceptionCodeEnum;
import com.ajmd.onlineexam.enumeration.StaticFinalValue;
import com.ajmd.onlineexam.service.ExamService;
import com.ajmd.onlineexam.service.PersonExamService;
import com.ajmd.onlineexam.vo.ExamQuestionVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author ajmd
 * @since 2023/5/9 17:45
 */
@RestController
@RequestMapping("/personExam")
public class PersonExamController {
    @Resource
    private PersonExamService personExamService;
    @Resource
    private ExamService examService;

//    @PostMapping
//    public CommonResult findPage(@MultiRequestBody String account,@MultiRequestBody Integer currentPage,
//                                  @MultiRequestBody Integer pageSize,@MultiRequestBody SearchParam searchParam){
//        personExamService.getPage(account,currentPage,pageSize,searchParam);
//
//        return CommonResult.ok();
//    }


    @PostMapping("/enter")
    public CommonResult enterExam(@MultiRequestBody String account,@MultiRequestBody String examId){
        //查询examPO，如果未达到开考时间，直接返回错误信息
        ExamPO exam = examService.getOne(examId);
        Timestamp now = new Timestamp(System.currentTimeMillis());
        if(exam.getStartTime().after(now)){
            return CommonResult.error(AppExceptionCodeEnum.EXAM_NOT_STARTED);
        }

        //获取该考生该场考试的记录
        List<PersonExamPO> personExamPOList = personExamService.getList(account, examId);
        Integer times = personExamPOList.size();
        if(times.equals(0)){
            //生成考试时已初始化，至少会有一条记录，如果没有则非本场考试考生
            return CommonResult.error(AppExceptionCodeEnum.NOT_THE_SUBJECT_OF_EXAM);
        }
        //过滤掉未考的记录和进行中的记录
        List<PersonExamPO> hasFinishList = personExamPOList.stream().filter(item ->
                item.getStatus() != StaticFinalValue.UNEXAMINED && item.getStatus() != StaticFinalValue.TESTING
        ).collect(Collectors.toList());
        if(exam.getLimitTimes() <= hasFinishList.size()){
            //考试次数用尽
            return CommonResult.error(AppExceptionCodeEnum.REACH_MAX_LIMIT_TIMES);
        }
        StringBuffer personExamId = new StringBuffer();
        //修改或新增人员考试表
        Timestamp enterTime = personExamService.updateOrAddPersonExam(account, examId, personExamPOList, personExamId);
        //异步任务，计时结束后执行修改考生考试状态

        //查询考试试题
        List<ExamQuestionVO> examQuestionVOS = personExamService.getExamQuestion(examId);

        return CommonResult.ok().data("dataList",examQuestionVOS)
                .data("enterTime",enterTime)
                .data("personExamId",personExamId);
    }

    @PostMapping("/savePersonAnswer")
    public CommonResult savePersonAnswer(@RequestParam String account, @RequestParam String personExamId,
                                         @RequestBody List<PersonAnswerPO> personAnswerList,
                                         @RequestParam boolean submitFlag,
                                         @RequestParam String examId){
//        if(ObjectUtils.isEmpty(personAnswerList)){
//            return CommonResult.error().message("未做任何更新");
//        }

        int num = personExamService.saveAnswer(account,personExamId,personAnswerList,submitFlag,examId);
        if(num==0){
            return CommonResult.error().message(submitFlag?"交卷失败":"试卷保存失败");
        }else if(num==-1){
            return CommonResult.error().message("本次考试已结束");
        }
        return CommonResult.ok().message(submitFlag?"交卷成功":"已保存");
    }
}
